package com.kclm.xsap.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kclm.xsap.entity.ReservationRecordEntity;
import com.kclm.xsap.entity.ReservationRecordEntity22;
import com.kclm.xsap.service.ReservationRecordService;
import com.kclm.xsap.utils.R;
import com.kclm.xsap.utils.exception.GlobalExceptionControllerAdvice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.stream.Collectors;

/**
 * 预约控制器
 */
@Controller
@RequestMapping("/reserve")
@Slf4j
public class ReservationRecordController {
    @Autowired
    private ReservationRecordService reservationRecordService;

    @PostMapping("/getReserveId.do")
    @ResponseBody
    public R getReserveId(@RequestParam("memberId") Long memberId,
                          @RequestParam("scheduleId") Long scheduleId) {
        System.out.println(memberId);
        System.out.println(scheduleId);
        ReservationRecordEntity reservationRecordEntity = reservationRecordService.getOne(new QueryWrapper<ReservationRecordEntity>()
                .eq("member_id", memberId)
                .eq("schedule_id", scheduleId));
        System.out.println(reservationRecordEntity.getId());
        return R.ok().put("id", reservationRecordEntity.getId());

    }

    /**
     * 添加会员预约记录
     */
    @PostMapping("/addReserve.do")
    @ResponseBody
    public R addReserve(ReservationRecordEntity reservationRecordEntity, HttpServletRequest request) {
        String startDate = request.getParameter("startDate");
        System.out.println("startDate:" + startDate);
        log.info("reservationRecordEntity====>" + reservationRecordEntity);
        try {
            reservationRecordService.isAdd(reservationRecordEntity, startDate);
            reservationRecordService.isInfo(reservationRecordEntity, startDate);
            return R.ok();
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 取消会员预约记录
     */
    @PostMapping("/cancelReserve.do")
    @ResponseBody
    public R cancelReserve(Long reserveId) {
        System.out.println(reserveId);
        //全局预约设置
        boolean isCancel = reservationRecordService.isCancel(reserveId);
        if (isCancel) {
            try {
                reservationRecordService.isCanInfo(reserveId);
                return R.ok();
            } catch (Exception e) {
                return R.error(e.getMessage());
            }
        } else {
            return R.error("取消截至时间已过，无法取消！");
        }

    }
}
